package plot;

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.ui.RectangleInsets;

import view.Environnement;


@SuppressWarnings("serial")
public class LineChart extends JPanel {

    private TimeSeries monkey;
    private TimeSeries food;
    Environnement e;
    LineChart panel;
    
    public LineChart(int maxAge, Environnement envi) {

        super(new BorderLayout());
        e = envi;
        this.monkey = new TimeSeries("Monkeys");
        this.monkey.setMaximumItemAge(maxAge);
        this.food = new TimeSeries("Food");
        this.food.setMaximumItemAge(maxAge);
        TimeSeriesCollection dataset = new TimeSeriesCollection();
        dataset.addSeries(this.monkey);
        dataset.addSeries(this.food);

        DateAxis domain = new DateAxis();
        NumberAxis range = new NumberAxis();

        XYItemRenderer renderer = new XYLineAndShapeRenderer(true, false);
        renderer.setSeriesPaint(0, Color.red);
        renderer.setSeriesPaint(1, Color.green);
        
        renderer.setSeriesStroke(0, new BasicStroke(3f, BasicStroke.CAP_BUTT,
                BasicStroke.JOIN_BEVEL));
        renderer.setSeriesStroke(1, new BasicStroke(3f, BasicStroke.CAP_BUTT,
                BasicStroke.JOIN_BEVEL));
        XYPlot plot = new XYPlot(dataset, domain, range, renderer);
        domain.setAutoRange(true);
        domain.setLowerMargin(0.0);
        domain.setUpperMargin(0.0);
        domain.setTickLabelsVisible(true);
        range.setTickLabelsVisible(true);

        range.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        renderer.setBaseItemLabelsVisible(false);

        JFreeChart chart = new JFreeChart("Modelisation groupe de singes",
                new Font("SansSerif", Font.BOLD, 24), plot, true);
        
        chart.setTitle("Modelisation groupe de singes");
        chart.setBackgroundPaint(Color.blue);

        ChartUtilities.applyCurrentTheme(chart);
        chart.setBorderVisible(false);
        chart.setBorderPaint(null);
        domain.setTickLabelInsets(RectangleInsets.ZERO_INSETS);
        range.setTickMarksVisible(false);
        range.setTickLabelInsets(RectangleInsets.ZERO_INSETS);
        domain.setTickMarksVisible(false);
        chart.setPadding(RectangleInsets.ZERO_INSETS);
        

        ChartPanel chartPanel = new ChartPanel(chart, true);
        chartPanel.setBorder(null);
        add(chartPanel);

    }

    private void addTotalObservation(double y) {
    	this.monkey.add(new Millisecond(), y);
    }


    private void addFreeObservation(double y) {
        this.food.add(new Millisecond(), y);
    }

    /**
     * The data generator.
     */
    class DataGenerator extends Timer implements ActionListener {

        /**
         * Constructor.
         *
         * @param interval  the interval (in milliseconds)
         */
        DataGenerator(int interval) {
            super(interval, null);
            addActionListener(this);
        }

        /**
         * Adds a new free/total memory reading to the dataset.
         *
         * @param event  the action event.
         */
        public void actionPerformed(ActionEvent event) {
            long f = e.allFoodList.size();
            long t = e.monkeysList.size();
            addTotalObservation(t);
            addFreeObservation(f);
        }

    }
    
    public void update(Environnement env){
    	e = env;
    }

   
    public void main(Environnement envi) {
 
        JFrame frame = new JFrame("Modelisation");
        panel = new LineChart(30000, envi);
        frame.getContentPane().add(panel, BorderLayout.CENTER);
        frame.setBounds(200, 120, 600, 280);
        frame.setVisible(true);
        
        panel. new DataGenerator(700).start();

        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}